<style>
div.level-0 { }
div.level-1 { margin-left: 30px; }
div.level-2 { margin-left: 60px; }
img.permission-toggle:hover { cursor: pointer; }
img.hook-toggle:hover { cursor: pointer; }
</style>
<div class="acl_permissions">
    <h2>{{__('Permissions')}}</h2>

    <div class="navbar">
    <div class="navbar-inner">
        <?php echo $this->Html->link(__('Add Action'), array('controller' => 'acl_actions', 'action'=>'add'),array('class'=>'btn btn-primary')); ?>
    </div>
    </div>

    <table class="table" cellpadding="0" cellspacing="0">
    <?php
        $roleTitles = array_values($roles);
        $roleIds   = array_keys($roles);

        $tableHeaders = array(
            __('Id', true),
            __('Alias', true),
 			__('Oprations', true),
        );
        $tableHeaders = array_merge($tableHeaders, $roleTitles);
        $tableHeaders =  $this->Html->tableHeaders($tableHeaders);
        echo $tableHeaders;

        $currentController = '';
        foreach ($acos AS $id => $alias) {
            $class = '';
            if(substr($alias, 0, 2) == '__') {
                $level = 2;
                $class .= 'level-'.$level;
                $oddOptions = array('class' => 'hidden controller-'.$currentController1);
                $evenOptions = array('class' => 'hidden controller-'.$currentController1);
                $alias = substr_replace($alias, '', 0, 2);
            }
            elseif(substr($alias, 0, 1) == '_') {
                $level = 1;
                $class .= 'controller collapserow level-'.$level;
                $oddOptions = array('class' => 'controller-'.$currentController);
                $evenOptions = array('class' => 'controller-'.$currentController);
                $alias = substr_replace($alias, '', 0, 1);
                $currentController1 = $alias;
            } else {
                $level = 0;
                $class .= ' controller collapserow';
                $oddOptions = array();
                $evenOptions = array();
                $currentController = $alias;
            }

            
            $row = array(
                $id,
                $this->Html->div($class, $alias,array('data-alias'=>$alias,'escape'=>false)),
            );

			$row[] = $this->Html->link(__('delete'), array('controller' => 'acl_actions', 'action'=>'delete',$id),array('target'=>'_blank','class'=>'btn btn-warning btn-xs','onclick'=>'return confirm("Are you sure to delete this?");'));

            foreach ($roles AS $roleId => $roleTitle) {				
                //if ($level != 0) {                	
                    if ($roleId != 1) {
                    	if ($permissions[$id][$roleId] == 1) {
							$row[] = $this->Html->image($this->Html->url('/img/icons/tick.png'), array('class' => 'permission-toggle', 'data-aco_id' => $id, 'data-aro_id' => $rolesAros[$roleId]));
						} else {
							$row[] = $this->Html->image($this->Html->url('/img/icons/cross.png'), array('class' => 'permission-toggle', 'data-aco_id' => $id, 'data-aro_id' => $rolesAros[$roleId]));
						}
                    } else {
                        $row[] = $this->Html->image($this->Html->url('/img/icons/tick_disabled.png'), array('class' => 'permission-disabled'));
                    }
                //} else {
                //    $row[] = '';
                //}
            }

            echo $this->Html->tableCells(array($row), $oddOptions, $evenOptions);
			echo "\r\n";
        }

        echo $tableHeaders;
    ?>
    </table>
</div>
<script>

var AclPermissions = {};

AclPermissions.permissionToggle = function() {
	$('img.permission-toggle').unbind();
	$('img.permission-toggle').click(function() {
		var $this = $(this);
		var acoId = $this.data('aco_id');
		var aroId = $this.data('aro_id');

		// show loader
		$this.attr('src', BASEURL+'/img/ajax/circle_ball.gif');

		// prepare loadUrl
		var loadUrl = ADMIN_BASEURL+'/admin/acl/acl_permissions/toggle/';
		loadUrl    += acoId+'/'+aroId+'/';

		// now load it
		var target = $this.parent();
		$.post(loadUrl, null, function(data, textStatus, jqXHR) {
			target.html(data);
			AclPermissions.permissionToggle();
		});

		return false;
	});
}
AclPermissions.documentReady = function() {
	AclPermissions.permissionToggle();
	AclPermissions.tableToggle();
	$('tr:has(div.controller)').addClass('controller-row');
}

AclPermissions.tableToggle = function() {
	$('table div.controller').click(function() {
		$('.controller-'+$(this).text()).toggle();
		if ($(this).hasClass('expandrow')) {
			$(this).removeClass('expandrow');
			$(this).addClass('collapserow');
		} else {
			$(this).removeClass('collapserow');
			$(this).addClass('expandrow');
		}
	});
}


$(document).ready(function() {
	AclPermissions.documentReady();
});
</script>